package com.wing.system.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.wing.common.utils.JsonDealUtils;
import com.wing.common.utils.JsonResult;
import com.wing.enterprisecab.feign.EnterpriseCabClient;
import com.wing.enterprisecab.product.model.response.EnterpriseVO;
import com.wing.platform.feign.PlatformClient;
import com.wing.system.dao.SystemEnterpriseDao;
import com.wing.system.model.entity.SystemEnterprise;
import com.wing.system.model.request.SystemEnterpriseForm;
import com.wing.system.model.response.SystemEnterpriseVO;
import lombok.extern.slf4j.Slf4j;
import org.json.JSONException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 入驻企业-ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class SystemEnterpriseServiceImpl extends ServiceImpl<SystemEnterpriseDao, SystemEnterprise> implements SystemEnterpriseService {
   /* @Autowired
    private EnterpriseCabClient enterpriseCabClient;
    @Autowired
    private PlatformClient platformClient;*/
    @Override
    public int add(SystemEnterpriseForm systemEnterpriseForm) {

        //校验

        SystemEnterprise systemEnterprise =new SystemEnterprise();
        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(SystemEnterprise.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(systemEnterpriseForm, systemEnterprise, copyOptions);

        return baseMapper.insert(systemEnterprise);
    }

    @Override
    public List<SystemEnterpriseVO> list4app(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public Page<SystemEnterpriseVO> pageList4app(Page page, Map<String, Object> params) {
        List<SystemEnterpriseVO> list = baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public SystemEnterpriseVO get4appById(Long id) {
        return baseMapper.get4appById(id);
    }

    @Override
    public SystemEnterpriseVO get5appById(Long id) {
        return baseMapper.get5appById(id);
    }


    @Override
    public IPage<SystemEnterprise> selectPage(Page<SystemEnterprise> pg, QueryWrapper<SystemEnterprise> queryWrapper) {
        IPage<SystemEnterprise> systemEnterpriseIPage = baseMapper.selectPage(pg, queryWrapper);
       /* if(systemEnterpriseIPage.getRecords().size() ==0){
            EnterpriseVO enterprise = enterpriseCabClient.get().getData();
                JsonResult companyInfoByCode = platformClient.getCompanyInfoByCode(enterprise.getCode());
                if (companyInfoByCode.getCode()==200) {
                    Object data = companyInfoByCode.getData();
                    String s = JSON.toJSONString(data);
                    JSONObject jsonObject = JsonDealUtils.getNoNullValue(s);
                    SystemEnterprise systemEnterprise = JSON.parseObject(jsonObject.toJSONString(), SystemEnterprise.class);
                    baseMapper.insert(systemEnterprise);
                }
             systemEnterpriseIPage = baseMapper.selectPage(pg, queryWrapper);
        }*/
        return systemEnterpriseIPage;
    }
}
